package com.situ.blog.servlet;

import com.situ.blog.pojo.Auth;
import com.situ.blog.pojo.query.AuthQuery;
import com.situ.blog.service.IAuthService;
import com.situ.blog.service.impl.AuthServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.MyBeanUtil;
import com.situ.blog.util.PageResult;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/auth")
public class AuthServlet extends HttpServlet {
    private IAuthService authService = new AuthServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("AuthServlet.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.update");
        Auth auth = MyBeanUtil.copyToBean(req, Auth.class);
        authService.update(auth);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.selectById");
        String id = req.getParameter("id");
        Auth auth = authService.selectById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok(auth));
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        authService.updateStatus(id, Integer.parseInt(status));
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.add");
        Auth auth = MyBeanUtil.copyToBean(req, Auth.class);
        authService.add(auth);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    // /auth?method=selectByPage&page=1&limit=10&name=Java
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp)  {
        System.out.println("AuthServlet.selectByPage");
       /* String page = req.getParameter("page");
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : entries) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        try {
            BeanUtils.populate(AuthQuery.class, req.getParameterMap());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }*/
        AuthQuery authQuery = MyBeanUtil.copyToBean(req, AuthQuery.class);
        System.out.println(authQuery);
        PageResult pageResult = authService.selectByPage(authQuery);

        JSONUtil.toJSON(resp, pageResult);
    }

    //http://localhost:8080/auth?method=toList
    //http://localhost:8080/page/auth/update
    //http://localhost:8080/page/auth/list
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("AuthServlet.toUpdate");
        req.getRequestDispatcher("/WEB-INF/auth_update.jsp").forward(req, resp);
    }

    private void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("AuthServlet.toList");
        req.getRequestDispatcher("/WEB-INF/auth_list.jsp").forward(req, resp);
    }
}
